package com.zn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zn.Vo.CardAndCareerVo;
import com.zn.bean.Admin;
import com.zn.bean.Card;
import com.zn.bean.Career;
import com.zn.controller.CommonController;
import com.zn.mapper.CardMapper;
import com.zn.service.AdminService;
import com.zn.service.CardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zn.service.CareerService;
import com.zn.utils.Result;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 码农小周
 * @since 2023-05-12
 */
@Service
@Slf4j
public class CardServiceImpl extends ServiceImpl<CardMapper, Card> implements CardService {


    @Resource
    private CareerService careerService;

    @Resource
    private AdminService adminService;

    @Override
    public Result<Page<CardAndCareerVo>> pageInfo(Long page, Long pageSize, String name) {

        Page<Card> cardPage = new Page<>(page, pageSize);
        Page<CardAndCareerVo> cardAndCareerVoPage = new Page<>();

        LambdaQueryWrapper<Card> cardLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cardLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Card::getName, name)
                .orderByDesc(Card::getUpdateTime);

        this.page(cardPage, cardLambdaQueryWrapper);

        BeanUtils.copyProperties(cardPage, cardAndCareerVoPage, "records");

        List<CardAndCareerVo> CardAndCareerVoList = cardPage.getRecords().stream().map(item -> {

            CardAndCareerVo cardAndCareerVo = new CardAndCareerVo();
            BeanUtils.copyProperties(item, cardAndCareerVo);

            Career dbCareer = careerService.getById(item.getCareerId());
            Admin dbAdmin = adminService.getById(item.getAdminId());

            cardAndCareerVo.setUpdateName(dbAdmin.getAdminName());
            cardAndCareerVo.setCareerName(dbCareer.getCareerName());

            return cardAndCareerVo;

        }).collect(Collectors.toList());

        cardAndCareerVoPage.setRecords(CardAndCareerVoList);

        return Result.success(cardAndCareerVoPage);
    }

    @Override
    public Result<String> addCard(Card card) {


        if (null == card) {
            return Result.error(501, "参数不能为空");
        }

        if (StringUtils.isEmpty(card.getName())
                || StringUtils.isEmpty(card.getCorporateName())
                || StringUtils.isEmpty(card.getPhone())
                || StringUtils.isEmpty(card.getEmail())
                || StringUtils.isEmpty(card.getAddress())
                || StringUtils.isEmpty(card.getDetailedAddress())
        ) {
            return Result.error(501, "必填参数不能为空");
        }
        this.save(card);


        return Result.success("新增成功");
    }

    @Override
    public Result<String> editCard(Card card) {

        if (null == card) {
            return Result.error(501, "参数不能为空");
        }

        if (StringUtils.isEmpty(card.getName())
                || StringUtils.isEmpty(card.getCorporateName())
                || StringUtils.isEmpty(card.getPhone())
                || StringUtils.isEmpty(card.getEmail())
                || StringUtils.isEmpty(card.getAddress())
                || StringUtils.isEmpty(card.getDetailedAddress())
        ) {
            return Result.error(501, "必填参数不能为空");
        }

        boolean b = this.updateById(card);

        return b ? Result.success("修改成功") : Result.error(501, "修改失败");
    }

    @Override
    public Result<String> delCard(Long cardId) {
        if (null == cardId) {
            return Result.error(501, "删除信息不能为空");
        }

        boolean b = this.removeById(cardId);

        return b ? Result.success("删除成功") : Result.error(501, "删除失败");

    }

    @Override
    public Result<String> uploadExcel(List<CardAndCareerVo> cardAndCareerVoList) {
        if (0 == cardAndCareerVoList.size()) {
            return Result.error(501, "请上传表格");
        }
        List<Card> cardArrayList = cardAndCareerVoList.stream().map(item -> {

            Card card = new Card();

            BeanUtils.copyProperties(item,card);
//            根据职业名称获取职业Id
            LambdaQueryWrapper<Career> careerLambdaQueryWrapper = new LambdaQueryWrapper<>();
            careerLambdaQueryWrapper.eq(Career::getCareerName, item.getCareerName());

            Career dbCareer = careerService.getOne(careerLambdaQueryWrapper);
            card.setCareerId(dbCareer.getCareerId());

//          根据修改管理员姓名获取管理员Id
            LambdaQueryWrapper<Admin> adminLambdaQueryWrapper = new LambdaQueryWrapper<>();
            adminLambdaQueryWrapper.eq(Admin::getAdminName, item.getUpdateName()).last("limit 1");

            Admin dbAdmin = adminService.getOne(adminLambdaQueryWrapper);
            card.setAdminId(dbAdmin.getAdminId());

            return card;

        }).collect(Collectors.toList());

        log.info("cardArrayList:{}",cardArrayList);

        this.saveBatch(cardArrayList);
        return Result.success("上传成功");
    }
}
